#include "common/lang/comparator.h"
#include "common/lang/sstream.h"
#include "common/log/log.h"
#include "common/type/vector_type.h"
#include "common/value.h"
#include <cmath>
#include <vector>

// class VectorType {
int VectorType::compare(const Value &left, const Value &right) const
{
    ASSERT(left.attr_type() == AttrType::VECTORS, "left type is not vector");
    ASSERT(right.attr_type() == AttrType::VECTORS, "right type is not vector");

    // 获取向量的大小
    size_t left_size = left.value_.vector_value_->size(); // 解引用指针
    size_t right_size = right.value_.vector_value_->size(); // 解引用指针

    // 比较向量大小
    if (left_size < right_size) {
        return -1; // left is smaller
    } else if (left_size > right_size) {
        return 1; // left is larger
    }

    // 向量大小相等，逐元素比较
    for (size_t i = 0; i < left_size; ++i) {
        float left_val = (*left.value_.vector_value_)[i]; // 解引用指针并访问元素
        float right_val = (*right.value_.vector_value_)[i]; // 解引用指针并访问元素

        // 确保传递的是 float 类型的值的地址
        int cmp_result = common::compare_float((void *)&left_val, (void *)&right_val);
        if (cmp_result != 0) {
            return cmp_result; // 返回第一个不相等的比较结果
        }
    }

    return 0; // 两个向量相等
}

    RC VectorType::add(const Value &left, const Value &right, Value &result) const {
        const std::vector<float>& left_vec = left.get_vector();
        const std::vector<float>& right_vec = right.get_vector();

        if (left_vec.size() != right_vec.size()) {
            return RC::SCHEMA_FIELD_TYPE_MISMATCH; // 向量大小不一致
        }

        std::vector<float> sum_vec(left_vec.size());
        for (size_t i = 0; i < left_vec.size(); ++i) {
            sum_vec[i] = left_vec[i] + right_vec[i];
        }
        result.set_vector(sum_vec);
        return RC::SUCCESS;
    }

    RC VectorType::subtract(const Value &left, const Value &right, Value &result) const {
        const std::vector<float>& left_vec = left.get_vector();
        const std::vector<float>& right_vec = right.get_vector();

        if (left_vec.size() != right_vec.size()) {
            return RC::SCHEMA_FIELD_TYPE_MISMATCH; // 向量大小不一致
        }

        std::vector<float> diff_vec(left_vec.size());
        for (size_t i = 0; i < left_vec.size(); ++i) {
            diff_vec[i] = left_vec[i] - right_vec[i];
        }
        result.set_vector(diff_vec);
        return RC::SUCCESS;
    }

    RC VectorType::multiply(const Value &left, const Value &right, Value &result) const {
        const std::vector<float>& left_vec = left.get_vector();
        const std::vector<float>& right_vec = right.get_vector();

        if (left_vec.size() != right_vec.size()) {
            return RC::SCHEMA_FIELD_TYPE_MISMATCH; // 向量大小不一致
        }

        std::vector<float> prod_vec(left_vec.size());
        for (size_t i = 0; i < left_vec.size(); ++i) {
            prod_vec[i] = left_vec[i] * right_vec[i];
        }
        result.set_vector(prod_vec);
        return RC::SUCCESS;
    }

    RC VectorType::l2_distance(const Value &left, const Value &right, float &distance) const {
        const std::vector<float>& left_vec = left.get_vector();
        const std::vector<float>& right_vec = right.get_vector();

        if (left_vec.size() != right_vec.size()) {
            return RC::SCHEMA_FIELD_TYPE_MISMATCH; // 向量大小不一致
        }

        distance = 0.0f;
        for (size_t i = 0; i < left_vec.size(); ++i) {
            float diff = left_vec[i] - right_vec[i];
            distance += diff * diff;
        }
        distance = std::sqrt(distance);
        return RC::SUCCESS;
    }

    RC VectorType::cosine_distance(const Value &left, const Value &right, float &distance) const {
        const std::vector<float>& left_vec = left.get_vector();
        const std::vector<float>& right_vec = right.get_vector();

        if (left_vec.size() != right_vec.size()) {
            return RC::SCHEMA_FIELD_TYPE_MISMATCH; // 向量大小不一致
        }

        float dot_product = 0.0f;
        float left_norm = 0.0f;
        float right_norm = 0.0f;

        for (size_t i = 0; i < left_vec.size(); ++i) {
            dot_product += left_vec[i] * right_vec[i];
            left_norm += left_vec[i] * left_vec[i];
            right_norm += right_vec[i] * right_vec[i];
        }

        if (left_norm == 0 || right_norm == 0) {
            distance = 0.0f; // 防止除零
            return RC::SUCCESS;
        }

        distance = 1.0f - (dot_product / (std::sqrt(left_norm) * std::sqrt(right_norm)));
        return RC::SUCCESS;
    }

    RC VectorType::inner_product(const Value &left, const Value &right, float &result) const {
        const std::vector<float>& left_vec = left.get_vector();
        const std::vector<float>& right_vec = right.get_vector();

        if (left_vec.size() != right_vec.size()) {
            return RC::SCHEMA_FIELD_TYPE_MISMATCH; // 向量大小不一致
        }

        result = 0.0f;
        for (size_t i = 0; i < left_vec.size(); ++i) {
            result += left_vec[i] * right_vec[i];
        }
        return RC::SUCCESS;
    }

RC VectorType::set_value_from_str(Value &val, const std::string &data) const   {
    RC rc = RC::SUCCESS;
    std::stringstream deserialize_stream;
    deserialize_stream.clear();  // 清理stream的状态，防止多次解析出现异常
    deserialize_stream.str(data);
    
    std::vector<float> vec;
    std::string token;

    // 逐个读取以逗号分隔的浮点数
    while (std::getline(deserialize_stream, token, ',')) {
        try {
            float float_value = std::stof(token); // 转换为浮点数
            vec.push_back(float_value); // 添加到向量中
        } catch (const std::invalid_argument &) {
            rc = RC::SCHEMA_FIELD_TYPE_MISMATCH; // 如果转换失败，返回错误
            break;
        } catch (const std::out_of_range &) {
            rc = RC::SCHEMA_FIELD_TYPE_MISMATCH; // 如果数值超出范围，返回错误
            break;
        }
    }

    if (rc == RC::SUCCESS) {
        val.set_vector(vec); // 假设你有一个set_vector函数
    }
    return rc;
}

RC VectorType::to_string(const Value &val, std::string &result) const {
    std::stringstream ss;

    // 确保 val 的类型是 VECTORS
    if (val.attr_type() != AttrType::VECTORS) {
        return RC::INVALID_ARGUMENT; // 返回 INVALID_ARGUMENT 如果类型不匹配
    }

    // 获取向量
    const std::vector<float> &vec = val.get_vector(); // 假设 get_vector 函数返回 std::vector<float>

    // 添加开括号
    ss << "[";
    
    // 将向量中的浮点数转换为字符串，以逗号分隔
    for (size_t i = 0; i < vec.size(); ++i) {
        ss << vec[i];
        if (i < vec.size() - 1) {
            ss << ","; // 添加逗号分隔
        }
    }

    // 添加闭括号
    ss << "]";

    result = ss.str(); // 将结果赋值给输出字符串
    return RC::SUCCESS; // 返回成功
}




